/*
编写 : Hexh
时间 : 2012-11-27 14:52:33
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FSBS.Core.Entity;
using FSBS.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsbsQmTrls
	/// </summary>
	public class FsbsQmTrlsController : BaseController
	{

		private readonly IFsbsQmTrlsFacade FsbsQmTrlsFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public FsbsQmTrlsController(IFsbsQmTrlsFacade FsbsQmTrlsFacade, IHospUserFacade HospUserFacade)
		{
			this.FsbsQmTrlsFacade = FsbsQmTrlsFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[ExtResult]
		[Description("添加修改FsbsQmTrls信息")]
		public ActionResult SaveOrUpdate()
		{
			FsbsQmTrls entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsbsQmTrls
				{
					Trsfid = Request["Trsfid"]==null ? 0 : int.Parse(Request["Trsfid"]), 
					CarId = Request["CarId"]==null ? 0 : int.Parse(Request["CarId"]), 
					Exusid = Request["Exusid"], 
					State = Request["State"], 
				};
			}
			else
			{
				// get the entity.
				entity = FsbsQmTrlsFacade.Get(Request["Id"]);
				// set new value..
				entity.Trsfid = Request["Trsfid"]==null ? 0 : int.Parse(Request["Trsfid"]); 
				entity.CarId = Request["CarId"]==null ? 0 : int.Parse(Request["CarId"]); 
				entity.Exusid = Request["Exusid"]; 
				entity.State = Request["State"]; 
			}			
			// TODO ..设置外键相关信息..
			/*
			string[] authusers = Request["AuthorizeUsers"].Split(',');
			for (int i = 0; i < authusers.Length; i++)
			{
				entity.AuthorizeUsers.Add(HospUserFacade.Load(authusers[i]));
			}
			*/
			// 保存信息
			FsbsQmTrlsFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除FsbsQmTrls基本资料")]
		public ActionResult Delete()
		{
			FsbsQmTrlsFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除FsbsQmTrls基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsbsQmTrlsFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsbsQmTrls清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsbsQmTrlsFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsbsQmTrls> { total = total, data = data },
				new string[] { "CustomType" });
		}
		
		[Description("获取FsbsQmTrls信息")]
		public ActionResult GetById()
		{
			var entity = FsbsQmTrlsFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsbsQmTrls> { data = new FsbsQmTrls[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断FsbsQmTrls字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsbsQmTrlsFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
